import $ from 'jquery-vendor'
import _ from 'lodash'
const api = {
  basePath: '/api_v3',
  checkIn: {
    name: '签到',
    path: '?m=sign&a=index',
    options: {
      uid: '1'
    }
  },
  isChecked: {
    name: '查看签到状态',
    path: '?m=sign&a=issign',
    options: {
      uid: '1'
    }
  },
  indexVision: {
    name: '获取当前版本号',
    path: '/?m=index&a=vision',
    options: {
      uid: '1'
    }
  },
  accountRentlist: {
    name: '转租列表',
    path: '/?m=account&a=rentlist',
    options: {
      uid: '1'
    }
  },
  accountDorent: {
    name: '提交转租',
    path: '/?m=account&a=dorent',
    options: {
      uid: '1',
      pid: 1,
      username: '111111',
      password: '222222',
      days: 1,
      wechat: '123123'
    }
  },
  accountNeedslist: {
    name: '寻租列表',
    path: '/?m=account&a=needslist',
    options: {
      uid: '1'
    }
  },
  accountDoneeds: {
    name: '提交寻租',
    path: '/?m=account&a=doneeds',
    options: {
      uid: '1',
      pid: 1
    }
  },
  accountList: {
    name: '购买平台列表',
    path: '/?m=account&a=list',
    options: {
      uid: '1'
    }
  },
  accountDaylist: {
    name: '购买账号列表',
    path: '/?m=account&a=daylist',
    options: {
      uid: '1',
      pid: 1
    }
  },
  accountDetail: {
    name: '订单详情',
    path: '/?m=account&a=detail',
    options: {
      uid: '1',
      orderid: '46552439332'
    }
  },
  payOneday: {
    name: '下订单',
    path: '/?m=pay&a=oneday',
    options: {
      uid: '1',
      aid: 5
    }
  },
  payPayok: {
    name: '支付成功',
    path: '/?m=pay&a=payok',
    options: {
      orderid: '46552439332',
      payorderid: '333333',
      paytype: 'alipay',
      ordersign: '123456'
    }
  },
  userIndex: {
    name: '首页数据',
    path: '/?m=user&a=index',
    options: {
      uid: '1'
    }
  },
  userOrderlist: {
    name: '购买记录',
    path: '/?m=user&a=orderlist',
    options: {
      uid: '1'
    }
  },
  userWithdrawals: {
    name: '申请提现页面',
    path: '/?m=user&a=withdrawals',
    options: {
      uid: '1'
    }
  },
  userDowithdrawals: {
    name: '申请提现功能',
    path: '/?m=user&a=dowithdrawals',
    options: {
      uid: '1',
      alipay: '123@123.cc'
    }
  },
  userVlist: {
    name: 'V级列表',
    path: '/?m=user&a=vlist',
    options: {
      uid: '1'
    }
  },
  userIncome: {
    name: '收入列表',
    path: '/?m=user&a=income',
    options: {
      uid: '1'
    }
  },
  userUcenter: {
    name: '用户中心',
    path: '/?m=user&a=ucenter',
    options: {
      uid: '1'
    }
  },
  userMsglist: {
    name: '消息列表',
    path: '/?m=user&a=msglist',
    options: {
      uid: '1'
    }
  },
  userMsgdetail: {
    name: '消息详情',
    path: '/?m=user&a=msgdetail',
    options: {
      uid: '1',
      messageid: 1
    }
  },
  userRentaccount: {
    name: '转租账号管理',
    path: '/?m=user&a=rentaccount',
    options: {
      uid: '1'
    }
  },
  userRentaccountdetail: {
    name: '转租账号平台详情',
    path: '/?m=user&a=rentaccountdetail',
    options: {
      uid: '1',
      pid: 1
    }
  },
  accountAccountdetail: {
    name: '账号详情',
    path: '/?m=account&a=accountdetail',
    options: {
      uid: '1',
      aid: 5
    }
  },
  vmoneyVmoneyRecharge: {
    name: 'V币充值界面',
    path: '/?m=vmoney&a=vmoneyrecharge',
    options: {
      uid: '1'
    }
  },
  vmoneyPayvm: {
    name: '充值V币接口',
    path: '/?m=vmoney&a=payvm',
    options: {
      uid: '1',
      vid: 5
    }
  },
  vmoneyPayvmok: {
    name: '支付成功',
    path: '/?m=vmoney&a=payvmok',
    options: {
      orderid: '46552439332',
      payorderid: '333333',
      paytype: 'alipay',
      ordersign: '123456'
    }
  },
  payVmoneyday: {
    name: '使用V币下账号订单',
    path: '/?m=pay&a=vmoneyday',
    options: {
      uid: '1',
      aid: 5
    }
  },
  payVmpayok: {
    name: 'V币购买账号支付成功',
    path: '/?m=pay&a=vmpayok',
    options: {
      orderid: '46552439332',
      payorderid: '333333',
      paytype: 'alipay',
      ordersign: '123456'
    }
  },
  /**
   * 调用接口
   * @param  {String} apiName    接口名
   * @param  {Object} apiOptions 接口传参
   * @return {Object}            promise
   */
  apiCall (apiName, apiOptions = {}) {
    const basePath = this.basePath
    const apiPath = this[apiName].path
    const options = $.extend(this[apiName].options, apiOptions)
    // 添加签名
    // 规则: 求用户id倒序的数字与19920629的和,再倒序
    if (!_.isEmpty(options.uid)) {
      options.sign = (Number(options.uid.split('').reverse().join('')) + 19920629).toString().split('').reverse().join('')
    }
    const loading = $('#loading')
    return $.ajax({
      url: basePath + apiPath,
      dataType: 'json',
      data: options,
      beforeSend () {
        loading.show()
      },
      success (data) {
        loading.hide()
      },
      error (err) {
        loading.hide()
        console.error(`Error! \nName: ${apiName}\nPath: ${apiPath}\nStatus: ${err.status} \nDetails: ${err.responseText}`)
      }
    })
  }
}

export default api
